import {Component, OnInit} from '@angular/core';
import {ActivatedRoute, Router} from "@angular/router";
import {HttpClient} from "@angular/common/http";
import {PaygoodsService} from "../services/paygoods.service";

@Component({
  selector: 'app-paydetail',
  templateUrl: './paydetail.component.html',
  styleUrls: ['./paydetail.component.scss']
})
export class PaydetailComponent implements OnInit {
  public title: any = '订单详情';
  public cancel: any = 'assets/cancel/u988.png';
  // 订单中商品的总件数
  public num: any = 0;
  // 接收到商品id
  public goodsId: any;
  // 接收到商品的信息
  public goodsMsg: any = [];
  // 接收到商品的数量
  public goodsCount: any = 1;
  // 总价
  public countPrice: any = 0;
  // 配送费
  public deliveryPrice: any = 10;
  // 折扣总价
  public lowPrice: any = 0;
  // 实付价钱
  public realPay: any = 0;
  // 遍历地址
  public addressMsg: any;
  public receiveName: any = '';
  public receiveTel: any = '';
  public provinceName: any = '';
  public cityName: any = '';
  public area: any = '';
  public details: any = '';
  constructor( private route: ActivatedRoute, private router: Router, private http: HttpClient, private service: PaygoodsService) {
  }

  ngOnInit() {
    this.goodsMsg = [];
    this.num = 0;
    this.countPrice = 0;
    this.realPay = 0;
    this.lowPrice = 0;
    if ( this.service.getGoodsmsg() !== null) {
      // 说明是从购物车来的
      this.goodsMsg = this.service.getGoodsmsg();
      for ( let i = 0; i < this.goodsMsg.length; i ++) {
        this.num = this.num + this.goodsMsg[i].goods_count;
      }
      // 算价钱
      this.getCountPrice( this.goodsMsg);
    } else {
      // 从商品详情来的
      this.goodsId = this.route.queryParams['value'].goods_id;
      this.goodsCount = this.route.queryParams['value'].goods_count;
      this.num = this.goodsCount;
      // 根据商品id请求商品
      const url = 'http://192.168.2.228:8080/goods/list';
      this.http.get(url).subscribe( res => {
        let msgArr = res['data']['list'];
        for ( let i = 0; i < msgArr.length; i ++) {
          if ( this.goodsId == msgArr[i]['g_id']) {
            msgArr[i]['goods_count'] = this.num
            this.goodsMsg.push(msgArr[i]);
            this.getCountPrice(this.goodsMsg);
          }
        }
      });
    }
    // 请求默认地址
    const api = 'http://192.168.2.228:8080/receive/list?receive_userid=' + localStorage.user;
    this.http.get(api).subscribe( res => {
      const arr = res['data'];
      console.log(arr);
      for ( let i = 0; i < arr.length; i++) {
        if ( arr[i] !== null) {
          if (arr[i].isdefault === 1) {
            this.addressMsg = arr[i];
            this.receiveName = this.addressMsg['receive_name'];
            this.receiveTel = this.addressMsg['receive_tel'];
            this.provinceName = this.addressMsg['province_name'];
            this.cityName = this.addressMsg['city_name'];
            this.area = this.addressMsg['area'];
            this.details = this.addressMsg['details'];
          }
        }
      }
    });

  }
  goDetail(k) {
      this.router.navigate(['/goodsdetails', this.goodsMsg[k].g_id]);
  }
  getCountPrice(arr) {
    for ( let i in arr) {
      // 原价
      this.countPrice = this.countPrice + arr[i].g_price * arr[i].goods_count;
      // 折扣总价
      this.lowPrice = this.lowPrice + arr[i].g_discount;
    }
    this.realPay = this.countPrice + this.deliveryPrice - this.lowPrice;
  }
  orderPay() {
    // 清空session
    this.service.delGoodsmsg();
  }
  goAddress() {
    this.router.navigate(['/address']);
  }

}
